<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020-08-10 14:17:16
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\ExcelService;
use app\common\controller\AdminBaseController;
use Jrk\Tool;
use think\Exception;
use think\facade\Db;
use app\common\service\FormBuilder as Form;
use think\facade\Route;
use think\Request;
use app\admin\model\Friendlinks;

class Friendlink extends AdminBaseController
{

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->model = new Friendlinks();
        /*所属平台*/
        $this->assign("site_link", config("admin.site_link"));
    }


    /**
     * @param Request $request
     * @return string|\think\response\Json
     * @throws \FormBuilder\exception\FormBuilderException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:添加
     */
    public function add(Request $request)
    {
        $f = [];
        $f[] = Form::hidden("__token__", token());
        $f[] = Form::select('site_link', '所属平台')->setOptions(function () {
            $tree = config("admin.site_link");
            $options = [];
            foreach ($tree as $k => $v) {
                $options[] = ['label' => $v, 'value' => $k];
            }
            return $options;
        })->multiple(0);
        $f[] = Form::input('name', '友链名称')->placeholder("请输入友情链接名称")->required("请输入友情链接名称");
        $f[] = Form::input('url', '友链地址')->placeholder("请输入友情链接地址")->required("请输入友情链接地址");
        $f[] = Form::radio('status', '状态', 1)->options([['label' => '正常', 'value' => 1], ['label' => '禁止', 'value' => 0]])->col(12);
        $form = Form::make_post_form('添加友情链接', $f, Route::buildUrl('upAndAdd'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


    /**
     * @param $id
     * @return string|\think\response\Json
     * @throws \FormBuilder\exception\FormBuilderException
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:编辑
     */
    public function edit($id)
    {

        $info = $this->model->where("id", $id)->find()->toArray();
        if (!$info) {
            return parent::failed("未查询到数据");
        }
        //dd($info);
        $f = [];
        $f[] = Form::hidden("__token__", token());
        $f[] = Form::select('site_link', '所属平台',"{$info['site_link']}")->setOptions(function () {
            $tree = config("admin.site_link");
            $options = [];
            foreach ($tree as $k => $v) {
                $options[] = ['label' => $v, 'value' => $k];
            }
            return $options;
        })->multiple(0);
        $f[] = Form::hidden("id", $info['id']);
        $f[] = Form::input('name', '友链名称', $info['name'])->placeholder("请输入友情链接名称")->required("请输入友情链接名称");
        $f[] = Form::input('url', '友链地址', $info['url'])->placeholder("请输入友情链接地址")->required("请输入友情链接地址");
        $f[] = Form::radio('status', '状态', $info['status'])->options([['label' => '正常', 'value' => 1], ['label' => '禁止', 'value' => 0]])->col(12);
        $form = Form::make_post_form('添加友情链接', $f, Route::buildUrl('upAndAdd'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


    /**
     * @return \think\response\Json
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:数据导出
     */
    public function export()
    {
        $param = $this->request->post();
        $where = [];
        $order = 'id desc';
        if (isset($param['name']) && $param['name'] != '') {
            $where[] = ['name', 'like', "%" . $param['name'] . "%"];
        }

        if (!empty($param['isAsc']) && !empty($param['orderByColumn'])) {
            $order = "{$param['orderByColumn']} {$param['isAsc']}";
        }

        if (!empty($param['site_link'])){
            $where[] = ['site_link', '=', (int)$param['site_link']];
        }

        if (!empty($param['status'])) {
            $status = (int)$param['status'] - 1;
            $where[] = ['status', '=', $status];
        }

        if (isset($param['time']) && $param['time'] != '') {
            $ck = @explode(" ~ ", $param['time']);
            $b = $ck[0] . " 00:00:00";
            $e = $ck[1] . " 23:59:59";
            $where[] = ['create_time', 'between', [strtotime($b), strtotime($e)]];
        }
        $result = $this->model->where($where)->order($order)->select()->toArray();
        if (empty($result)) {
            return parent::JsonReturn("根据条件未查询到数据", 0);
        }

        $arr = [];
        $ar=config("admin.site_link");
        foreach ($result as $k => $v) {
            $arr[] = [
                $v['id'], $v['name'], $v['url'], $v['admin_id'],$ar[$v['site_link']], $v['create_time']
            ];
        }
        $filename = ExcelService::setExcelHeader(['ID', '友链名称', '友链地址', '后台ID', '所属平台', '时间'])
            ->setExcelTile('友链数据', '友链数据', date('Y-m-d H:i:s', time()))
            ->setExcelContent($arr)
            ->ExcelReturn();
        return parent::JsonReturn($filename);
    }

}